package com.youku.expression.ast;

import com.youku.expression.Token;

public abstract class OpArithmeticAst extends OpAst{

	public OpArithmeticAst(Token op, Ast exp1, Ast exp2) {
		super(op, exp1, exp2);
	}

	@Override
	protected Object operate(Object former, Object latter) {
		if(former instanceof Number && latter instanceof Number){
			Number fm=(Number)former;
			Number lt=(Number)latter;
			if(fm instanceof Double||lt instanceof Double){
				return compute(fm.doubleValue(), lt.doubleValue());
			}else if(fm instanceof Float||lt instanceof Float){
				return  compute(fm.floatValue(), lt.floatValue());
			}else if(fm instanceof Long||lt instanceof Long){
				return compute(fm.longValue(), lt.longValue());
			}else{
				return compute(fm.intValue(), lt.intValue());
			}
		}
		throw new RuntimeException("非数字类型，无法做算术运算");
	}
	
	protected abstract double compute(double a,double b);
	
	protected abstract float compute(float a,float b);
	
	protected abstract long compute(long a,long b);
	
	protected abstract int compute(int a,int b);

}
